package model;

import parsing.RaceResultParsingInformation;
import utility.Utility;

/**
 * This is one of the groups within the race. It's a gender, age range, and has a count. 
 * For instance, there might be 123 women between 46 and 50 years of age. 
 * This will parse out count times per race but we really only need one copy of each per race.
 * Should be kept in a set - if used at all. 
 * @author User
 *
 */
public class Cohort {
	private int minAge ; 
	private int maxAge ; 
	private int count ; 
	private boolean isMale ;
	private int id ; // The id generated when this is put in to the database.
	
	/**
	 * This relies on a group format of "(M|F)25/29" or something like that.
	 * Should have separate columns for minimum age, maximum age, etc. 
	 * @param dataString
	 * @param parsingInformation
	 */
	public Cohort(String dataString, RaceResultParsingInformation parsingInformation) {
		int minStart = parsingInformation.getGroupNameStart() + 1 ; 
		int minEnd = minStart + 2 ; 
		minAge = Utility.getInt(dataString.substring(minStart, minEnd)) ;
		int maxStart = minEnd + 1 ; 
		int maxEnd = maxStart + 2 ; 
		maxAge = Utility.getInt(dataString.substring(maxStart, maxEnd)) ;
		this.isMale = "M".equals(dataString.substring(parsingInformation.getGroupNameStart(), parsingInformation.getGroupNameStart() + 1)) ;
		this.count = Utility.getInt(dataString.substring(parsingInformation.getGroupSizeStart(), parsingInformation.getGroupSizeEnd()).trim()) ;
	}
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + maxAge;
		result = prime * result + minAge;
		if(isMale) {
			result *= 37 ; 
		}
		return result;
	}

	@Override
	public boolean equals(Object object) {
		if (this == object)
			return true;
		if (object == null)
			return false;
		if (!(object instanceof Cohort))
			return false;
		Cohort other = (Cohort) object;
		if (isMale != other.isMale)
			return false;
		if (maxAge != other.maxAge)
			return false;
		if (minAge != other.minAge)
			return false;
		return true;
	}

	public int getMinAge() {
		return minAge;
	}

	public int getMaxAge() {
		return maxAge;
	}

	public int getCount() {
		return count;
	}

	public boolean isMale() {
		return isMale;
	}

	public String getGender() {
		String gender = "F" ;
		if(isMale) {
			gender = "M" ; 
		}
		return gender ;
	}

	public int getId() {
		return this.id ; 
	}
	
	public void setId(int cohortId) {
		this.id = cohortId ; 
	}

	/*
	 * Can't check gender cause it always has some value.
	 * Can't check id cause it may not be set for good reason. 
	 */
	public boolean isValid() {
		boolean isValid = false ;
		isValid = this.minAge >= 0 ; 
		isValid = isValid && this.maxAge >= 0 ; 
		return isValid ;
	}

	/**
	 * 
	 * @return true if the person belongs to this cohort. 
	 * Normally that means is of the age range and gender of that cohort. 
	 */
	public boolean fitsPerson(Runner person, int currentYear) {
		boolean isAFit = person.isMale() == this.isMale ;
		int personAge = person.getAge(currentYear) ; 
		isAFit = isAFit && personAge >= this.minAge ; 
		isAFit = isAFit && personAge <= this.maxAge ; 
		return isAFit ;
	}
	
}
